package com.changgou.canal.listen;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.util.CanalUtil;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.ListenPoint;

import java.util.Map;

@CanalEventListener
public class CanalListen {


    /**
     * @InsertListenPoint 增加之前监听  获取增加后的数据
     * @param evenType  当前操作的类型   增加数据
     * @param rowData   发生变更的每一行
     */
    @InsertListenPoint
    public void onEventInsert(CanalEntry.EntryType evenType,CanalEntry.RowData rowData){
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            //每一列column
            System.out.println("增加后数据:===>列名" + column.getName() + "====属性值" + column.getValue());
        }

    }

    @ListenPoint(
            //指定监听操作类型
            eventType={CanalEntry.EventType.DELETE,CanalEntry.EventType.UPDATE},
            //指定数据库
            schema={"changgou_content"},
            //指定某个表
            table={"tb_content"},
            //指定之前配置的实例example
            destination = "example"
    )
    public void onEventCustomUpdate(CanalEntry.EntryType evenType,CanalEntry.RowData rowData){
        Map<String, String> beforeMap = CanalUtil.convertoMap(rowData.getBeforeColumnsList());

        System.out.println("修改前数据"+ JSON.toJSONString(beforeMap));

        Map<String, String> afterMap = CanalUtil.convertoMap(rowData.getAfterColumnsList());

        System.out.println("修改后数据"+JSON.toJSONString(afterMap));

    }



}
